#!/bin/bash
# Script to automatically build db with mock inserts option

#config information

#Server Root Path
serverPath="/opt/lampp/htdocs/TDDP1-Server"

dbName="TDDP1Server"
dbUserName="root"
dbPassword="developer"

logPath="/opt/lampp/logs/tddp1-mysql.log"
mySqlBin="/opt/lampp/bin/mysql"

#end config information

touch $logPath
 
echo "CREATE DATABASE IF NOT EXISTS $dbName;" | sudo "$mySqlBin" -u"$dbUserName" -p"$dbPassword"

# Run initial dump
sudo "$mySqlBin" "$dbName" --user="$dbUserName" --password="$dbPassword" < "$serverPath/db/TDDP1Server.sql" 2>&1 | tee -a "$logPath"

# Run updates
IFS=$'\n'
for file in `ls -r --sort=time "$serverPath/db/updates"`; do
	sudo "$mySqlBin" "$dbName" --user="$dbUserName" --password="$dbPassword" < "$serverPath/db/updates/$file" 2>&1 | tee -a "$logPath"
done


if [ "$1" == "-i" ]; then
	# Run mock inserts
	IFS=$'\n'
	for file in `ls -r --sort=time  "$serverPath/db/mock_inserts" | grep .sql`; do
		sudo "$mySqlBin" "$dbName" --user="$dbUserName" --password="$dbPassword" < "$serverPath/db/mock_inserts/$file" 2>&1 | tee -a "$logPath"
	done
fi



